﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FillCartridgeBBB.Models
{
    public class CartridgeRepository
    {
        private FillCartridgeDbEntities _db;

        public CartridgeRepository(FillCartridgeDbEntities db)
        {
            _db = db;
        }

        //public IEnumerable<Cartridge> SelectAll(Func<Cartridge,bool> func)
        //{
        //    func()
        //    _db.CartridgeSet
        //    return _db.CartridgeSet.Include("Printer").Include("State");
        //}

        public IEnumerable<Cartridge> SelectAll()
        {
            return _db.CartridgeSet.Include("Printer").Include("State");
        }

        public IEnumerable<Cartridge> SelectEmpty ()
        {
            return _db.CartridgeSet.Include("Printer").Include("State").Where(cart => cart.State.StateId == 3);
        }

        public IEnumerable<Cartridge> SelectByState(State st)
        {
            return _db.CartridgeSet.Where(c => c.State.StateId == st.StateId);
        }

        public IEnumerable<Cartridge> SelectByPrinter(IEnumerable<Printer> printers)
        {
            List<Cartridge> cartridges = new List<Cartridge>();
            foreach (var cartridge in _db.CartridgeSet.Include("Printer"))
            {
                foreach (var printer in printers)
                {
                    if (cartridge.Printer.PrinterId == printer.PrinterId)
                    {
                        cartridges.Add(cartridge);
                    }
                }
            }

            return cartridges;
        }

        public Cartridge SelectById (int id)
        {
            return _db.CartridgeSet.Include("Printer").Include("State").First(c => c.CartridgeId == id);
        }

        public void Delete (int id)
        {
            _db.DeleteObject(SelectById(id));
            _db.SaveChanges();
        }

        public void Update (Cartridge cartridge, int printerId, int stateId)
        {
            Cartridge dbCart = SelectById(cartridge.CartridgeId);
            dbCart.State = _db.StateSet.First(s => s.StateId == stateId);
            dbCart.Printer = _db.PrinterSet.First(p => p.PrinterId == printerId);
            dbCart.SerialNumber = cartridge.SerialNumber;
            _db.SaveChanges();
        }

        public void Insert (Cartridge cartridge)
        {
            _db.AddToCartridgeSet(cartridge);
            _db.SaveChanges();
        }

        public void NextState (int id)
        {
            Cartridge dbCart = SelectById(id);
            int stateId = dbCart.State.StateId;
            dbCart.State = _db.StateSet.First(s=>s.StateId == stateId+1) != null ? _db.StateSet.First(s => s.StateId == stateId + 1) : _db.StateSet.First(s => s.StateId == 0);

        }

        public void PriorState (int id)
        {
            Cartridge dbCart = SelectById(id);
            int stateId = dbCart.State.StateId;
            dbCart.State = _db.StateSet.First(s => s.StateId == stateId - 1) != null ? _db.StateSet.First(s => s.StateId == stateId - 1) : _db.StateSet.First(s => s.StateId == (_db.StateSet.Max(st=>st.StateId)));
        }
    }
}
